package cn.easier.activity.console.controller;

import cn.easier.activity.console.service.ActivityInfoService;
import cn.easier.activity.console.service.ActivityLabelService;
import cn.easier.activity.console.service.ActivityTemplateInfoService;
import cn.easier.activity.web.entity.ActivityLabel;
import cn.easier.activity.web.entity.ActivityTemplateInfo;
import cn.easier.club.base.dto.ResultInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动营销平台模板展示Controller
 */
@Controller
@RequestMapping("/activity/template/show")
public class ActivityShowTemplateController {

    private static final Integer NEWEST_NUM = 3;
    private static final Integer HOTTEST_NUM = 3;
    private static Logger logger = LoggerFactory.getLogger(ActivityShowTemplateController.class);
    @Resource
    private ActivityLabelService activityLabelService;

    @Resource
    private ActivityTemplateInfoService activityTemplateInfoService;

    @Resource
    private ActivityInfoService activityInfoService;

    /**
     * 获取活动模板标签
     *
     * @return
     */
    @GetMapping("/activity-label")
    @ResponseBody
    public List getTemplateDictionary() {
        List showActivityLabelList = new ArrayList();
        List showDataActivityLabelList= new ArrayList();
        List<ActivityLabel> parentActivityLabelList = new ArrayList<ActivityLabel>();
        List<ActivityLabel> childActivityLabelList = new ArrayList<ActivityLabel>();
        List<ActivityLabel> activityLabelList = activityLabelService.findAll();
        logger.info("获取所有标签列表：activityLabelList size ==>{}", activityLabelList.size());

        //获取所有父标签
        for (ActivityLabel activityLabel : activityLabelList) {
            if (activityLabel.getParentId() == null) {
                parentActivityLabelList.add(activityLabel);
            }
        }

        //组装标签
        for (ActivityLabel parentActivityLabel : parentActivityLabelList) {
            Integer id = parentActivityLabel.getId();
            //组织当前父标签的子标签集合
            for (ActivityLabel activityLabel : activityLabelList) {
                if (activityLabel.getParentId() == id) {
                    childActivityLabelList.add(activityLabel);
                }
            }
            //添加标签
            //父标签 showData.get(0)
            showDataActivityLabelList.add(parentActivityLabel.getLabel());
            showDataActivityLabelList.add(parentActivityLabel.getValue());
            //子标签列表 showData.get(1)
            showDataActivityLabelList.add(childActivityLabelList);

            //返回的总集合
            showActivityLabelList.add(showDataActivityLabelList);
            //初始化子标签集合和某类型标签集合，进入循环
            childActivityLabelList = new ArrayList<ActivityLabel>();
            showDataActivityLabelList = new ArrayList();
        }

        for(int i = 0 ; i<showActivityLabelList.size(); i++){
            logger.info("返回标签类型顺序; => {}",((List)showActivityLabelList.get(i)).get(0));
        }
        logger.info("返回标签集合: =>{}", showActivityLabelList);
        return showActivityLabelList;
    }

    /**
     * 活动模板分页列表
     *
     * @return
     */
    @GetMapping("/activity-template-list")
    @ResponseBody
    public ResultInfo getActivityInfoList(@RequestParam(value = "scienceLabelIds", required = false) String scienceLabelIds,
                                          @RequestParam(value = "fromLabelIds", required = false) String fromLabelIds,
                                          @RequestParam(value = "themeLabelIds", required = false) String themeLabelIds,
                                          @RequestParam(value = "typeLabelIds", required = false) String typeLabelIds,
                                          @RequestParam(value = "pageNum", required = false, defaultValue = "0") int pageNum,
                                          @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        ResultInfo resultInfo = null;
        try {
            resultInfo = activityTemplateInfoService.findPage(scienceLabelIds, fromLabelIds, themeLabelIds, typeLabelIds, pageNum, pageSize);
            logger.info("查询结果：=>{}", resultInfo);
        } catch (Exception e) {
            logger.error("模板分页查询失败，=>{}", e);
            resultInfo = new ResultInfo("ERROR", "模板分页查询失败", null);
        }
        return resultInfo;
    }

    /**
     * 获取最新活动模板
     *
     * @return
     */
    @GetMapping("/newest")
    @ResponseBody
    public List<ActivityTemplateInfo> getNewest() {
        //获取最新模板top
        List<ActivityTemplateInfo> activityTemplateInfoTop = activityTemplateInfoService.getNewest(NEWEST_NUM);
        logger.info("获取最新活动模板top：==>{}", activityTemplateInfoTop);
        return activityTemplateInfoTop;
    }

    /**
     * 获取最热活动模板
     *
     * @return
     */
    @GetMapping("/hottest")
    @ResponseBody
    public List<ActivityTemplateInfo> getHottest() {
        List<ActivityTemplateInfo> activityTemplateInfoHottest = new ArrayList<ActivityTemplateInfo>();

        //在活动列表中获取使用率最高模板id
        List<Integer> activityTemplateIds = activityInfoService.getHottest(HOTTEST_NUM);
        //添加最热模板集合
        for (Integer activityTemplateId : activityTemplateIds) {
            if (activityTemplateId != null) {
                ActivityTemplateInfo templateInfo = activityTemplateInfoService.findOne(activityTemplateId);
                if (templateInfo != null) {
                    activityTemplateInfoHottest.add(templateInfo);
                } else {
                    logger.error("模板信息异常，找不到对应的模板信息。activityTemplateId：==>{}", activityTemplateId);
                }
            }
        }
        logger.info("获取最热活动模板top：==>{}", activityTemplateInfoHottest);
        return activityTemplateInfoHottest;
    }
}
